import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { NomicEmbeddings } from "./utils/embed.js";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";

const loader = new TextLoader("data/kong.txt");

const docs = await loader.load();

const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 64,
  chunkOverlap: 0,
});

const splittedDocs = await splitter.splitDocuments(docs);

const embeddings = new NomicEmbeddings(4);

const store = new MemoryVectorStore(embeddings);

await store.addDocuments(splittedDocs);

// // 创建一个检索器，现在仓库已经有值了
// const retriever = store.asRetriever(1);

// const res = await retriever.invoke("茴香豆是做什么用的");

// console.log(res);

// 做一个文档的过滤，只检索 kong.txt 文档
const onlyKongTxt = (doc) => doc.metadata?.source?.endsWith("data/kong.txt");

const res = await store.similaritySearchWithScore("茴香豆是做什么用的", 2, onlyKongTxt);

console.log(res);
